import cv2
import sys
import numpy as np
import datetime
import os
import glob
from RetinaFace.retinaface import RetinaFace
import time

thresh = 0.8
scales = [1080, 1980]

count = 2

gpuid = 0
detector = RetinaFace('../model/Retinaface/R50', 0, gpuid, 'net3')

img3_ = cv2.imread('t3.png')
img3 = cv2.resize(img3_, (int(1920 / 4), int(1080 / 4)))
img4_ = cv2.imread('t4.png')
img4 = cv2.resize(img4_, (int(1920 / 4), int(1080 / 4)))
img5_ = cv2.imread('t5.png')
img5 = cv2.resize(img5_, (int(1920 / 4), int(1080 / 4)))
img6_ = cv2.imread('t6.png')
img6 = cv2.resize(img6_, (int(1920 / 4), int(1080 / 4)))

faces, landmarks = detector.detect(img3, thresh, do_flip=False)
time1=time.time()

for i in range(100):
  faces, landmarks = detector.detect(img4, thresh, do_flip=False)
time2=time.time()
print('time used :%d '%(time2-time1))
if faces is not None:
  print('find', faces.shape[0], 'faces')
  for i in range(faces.shape[0]):
    color = (0, 0, 255)
    box = faces[i].astype(np.int)
    cv2.rectangle(img4, (box[0], box[1]), (box[2], box[3]), color, 2)
    #print('score', faces[i][4])
    box = faces[i].astype(np.int)*4
    #color = (255,0,0)
    cv2.rectangle(img4_, (box[0], box[1]), (box[2], box[3]), color, 2)
    if landmarks is not None:
      landmark5 = landmarks[i].astype(np.int)*4
      #print(landmark.shape)
      for l in range(landmark5.shape[0]):
        color = (0,0,255)
        if l==0 or l==3:
          color = (0,255,0)
        cv2.circle(img4_, (landmark5[l][0], landmark5[l][1]), 1, color, 2)

  filename1 = './detector_test_4.jpg'
  filename2 = './detector_test_4_.jpg'
  print('writing', filename1)
  cv2.imwrite(filename1, img4)
  cv2.imwrite(filename2, img4_)
print()

